package com.chenglin.picturesharedemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    private ConstraintLayout conShare;
    private ImageView ivCode;
    private Button btnShare;
    private boolean isHasScreenShotListener = false;
    private String path;


    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;

        conShare = findViewById(R.id.con_view);
        ivCode = findViewById(R.id.iv_code);
        btnShare = findViewById(R.id.btn_share);


        //分享 截图
        btnShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //获取 view的截屏
                Bitmap viewBitmap = ScreenShotUtil.getViewBitmap(conShare);
                //获取本地的二维码
                InputStream is = getResources().openRawResource(R.drawable.ic_code);
                Bitmap mBitmap = BitmapFactory.decodeStream(is);
                //将两张图片合并成一张
                Bitmap bitmap = mergeBitmap(viewBitmap, mBitmap);
                //将合成的图片保存在本地 Android/data/包名/file/ 文件下
                String picturePath = ScreenShotUtil.saveShotPicture(MainActivity.this, bitmap);
                if (picturePath !=null){
                    new PictureDialog(MainActivity.this,bitmap).show();
                }
            }
        });
    }

    // 两张图片合成一张图片
    private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
        Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(),firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);
        //画第二张图片的位置 (右下角)
        canvas.drawBitmap(secondBitmap, firstBitmap.getWidth()-secondBitmap.getWidth(), firstBitmap.getHeight()-secondBitmap.getHeight(), null);
        return bitmap;
    }



}